/**
 * @file RBG_device.c
 * @author Ai-Thinker Seahi
 * @brief
 * @version 0.1
 * @date 2021-08-26
 *
 * @copyright Copyright (c) 2021
 *
 */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "RGB_device.h"
#include "cJSON.h"
#include "app_mqtt.h"
 /**
  * @brief 定期RGB引脚的PWD号
  *
  */
static const uint32_t pin_num[3] = {
    Red_Pin,
    Green_Pin,
    Blue_Pin,
};
/**
 * @brief 设置红灯占空比参数
 *
 */
static uint32_t Red_dutie[3] = {
    800,1000,1000,
};
/**
 * @brief 设置绿灯占空比参数
 *
 */
static uint32_t Gerrn_dutie[3] = {
        1000,800,1000,
};
/**
 * @brief 设置蓝灯占空比参数
 *
 */
static uint32_t Blue_dutie[3] = {
        1000,1000,800,
};
/**
 * @brief 设置紫灯占空比参数
 *
 */
static uint32_t Violet_dutie[3] = {
        800,1000,500,
};
/**
 * @brief 设置黄灯占空比参数
 *
 */
static uint32_t Yellow_dutie[3] = {
        800,600,1000,
};
/**
 * @brief 关闭灯
 *
 */
static uint32_t Close_dutie[3] = {
        1000,1000,1000,
};
/**
 * @brief 设置PWD的相位
 *
 */
float phase[3] = {
    -90.0, -90.0, -90.0,
};
/**
 * @brief RGB 灯IO初始化
 *
 */
void rgb_io_init(void)
{
    pwm_init(PWM_PERIOD, Close_dutie, 3, pin_num);
    pwm_set_phases(phase);
    pwm_start();

}
/**
 * @brief 熄灭所有灯
 *
 */
void close_rgb()
{
    pwm_stop(0x2);
    pwm_set_period_duties(PWM_PERIOD, Close_dutie);
    pwm_start();
}
/**
 * @brief 设置颜色
 *
 */
void rgb_set_color(RGB_handle_t color)
{
    pwm_stop(0x2);
    RGB_STATUS = color;
    switch (color)
    {
        case RED:
            pwm_set_period_duties(PWM_PERIOD, Red_dutie);
            break;
        case GREEN:
            pwm_set_period_duties(PWM_PERIOD, Gerrn_dutie);
            break;
        case BULE:
            pwm_set_period_duties(PWM_PERIOD, Blue_dutie);
            break;
        case VIOLET:
            pwm_set_period_duties(PWM_PERIOD, Violet_dutie);
            break;
        case YELLOW:
            pwm_set_period_duties(PWM_PERIOD, Yellow_dutie);
            break;
        default:
            break;
    }
    pwm_start();
}
